<?php
/**
 * Created by PhpStorm.
 * User: hhlov_000
 * Date: 15-1-28
 * Time: 下午2:38
 */

namespace Admin\Controller;


class MissionController extends CommonController{

    /*
     *     发布的活首页
     *   * */
    public function missionList($page = 1, $rows = 10, $sort = 'mission_id', $order = 'asc'){
        if(IS_POST){
            $mission_db = M('mission');
            $total = $mission_db->count();
            $order = $sort.' '.$order;
            $limit = ($page - 1) * $rows . "," . $rows;
            //$list = $member_db->table(C('DB_PREFIX').'user M')->join(C('DB_PREFIX').'member_type MT on MT.typeid = M.typeid')->field("M.memberid,M.user_name,M.sex")->order($order)->limit($limit)->select();
            //$list = $member_db->field('user_id,user_name,sex,phone,status')->order($order)->limit($limit)->select();
            $list = $mission_db->join('user on mission.publisher_id = user.user_id','LEFT')
                ->field(array('mission_id,mission_name,jobs,duration,salary2,location2,user_name,publish_time,mission.status,valid_time,totop'))
                ->order($order)->limit($limit)->select();
            if(!$list) $list = array();
            $data = array('total'=>$total, 'rows'=>$list);
            $this->ajaxReturn($data);
        }else{
            $menu_db = D('Menu');
            $currentpos = $menu_db->currentPos(I('get.menuid'));  //栏目位置
            $datagrid = array(
                'options'     => array(
                    'title'   => $currentpos,
                    'url'     => U('Mission/missionList', array('grid'=>'datagrid')),
                    'toolbar' => 'mission_missionlist_datagrid_toolbar',
                ),
                'fields' => array(
                    '任务名称'      => array('field'=>'mission_name','width'=>15,'sortable'=>true),
                    '职位要求'    => array('field'=>'jobs','width'=>15,'sortable'=>true),
                    '工作地点'    => array('field'=>'location2','width'=>15,'sortable'=>true),
                    '工作时间'    => array('field'=>'duration','width'=>15,'sortable'=>true,'formatter'=>'missionDurationFormatter'),
                    '薪资' => array('field'=>'salary2','width'=>8,'sortable'=>true),
                    '发布时间' => array('field'=>'publish_time','width'=>15,'sortable'=>true),
                    '发布人'  => array('field'=>'user_name','width'=>8,'sortable'=>true),
                    '任务状态'    => array('field'=>'status','width'=>15,'sortable'=>true,'formatter'=>'missionStatusFormatter'),
                    '任务有效期'    => array('field'=>'valid_time','width'=>15,'sortable'=>true,'formatter'=>'missionValidTimeFormatter'),
                    '置顶'    => array('field'=>'totop','width'=>15,'sortable'=>true,'formatter'=>'missionTopFormatter'),
                    '管理操作'    => array('field'=>'mission_id','width'=>25,'formatter'=>'missionListOperateFormatter'),
                )
            );

            $this->assign('datagrid', $datagrid);
            $this->display('mission_list');
        }

    }

    public function missionView($mission_id){
        if(IS_POST){
            /*$field = array(
                'mission_name'    => '任务名称',
                'description'    => '详细描述',
                'usernumber'    => '应征人数',
                'selected'  => '应征成功用户',

                //'type'    => '来源',
                //'addtime' => '首次授权时间'
            );

            $mission_db = M('mission');
            $list = $mission_db->field(array('mission_name,description'))->where(array('mission_id'=>$mission_id))->select();
            $mission_user_db = M('miss_user_relate');
            $count_mission_user = $mission_user_db->where(array('mission_id'=>$mission_id))->count();
            $selected_users = $mission_user_db->join('user on miss_user_relate.follower_id = user.user_id','LEFT')
                ->field(array('user_name'))
                ->where(array('mission_id'=>$mission_id,'is_selected'=>1))->select();
            $users_array = array();
            foreach($selected_users as $user){
                $users_array[] = $user['user_name'];
            }
            $users = implode(',',$users_array);

            $data = array();

            if(sizeof($list)==1){
                $info = $list[0];
                $info['usernumber'] = $count_mission_user;
                $info['selected'] = $users;
                //$key = array_keys($info);
                //$value = array_values($info);
                foreach ($info as $key=>$value){


                    $data[] = array(
                        'name'    => $field[$key],
                        'group'   => strtoupper($info['mission_name']) . '任务基本信息',
                        'value'   => $value,
                    );
                }
           }

            $this->ajaxReturn($data);*/
        }else {
            /*$propertygrid = array(
                'options'     => array(
                    'url'     => U('Mission/missionView', array('mission_id'=>$mission_id, 'grid'=>'propertygrid')),
                )
            );
            $this->assign('propertygrid', $propertygrid);*/

            $mission_db = M('mission');
            $list = $mission_db->field(array('mission_name,description'))->where(array('mission_id'=>$mission_id))->select();
            $mission_user_db = M('miss_user_relate');
            //$count_mission_user = $mission_user_db->where(array('mission_id'=>$mission_id))->count();
            $apply_users = $mission_user_db->join('user on miss_user_relate.follower_id = user.user_id','LEFT')
                ->field(array('follower_id','user_name'))
                ->where(array('mission_id'=>$mission_id,'is_selected'=>1))->select();
            $selected_users = $mission_user_db->join('user on miss_user_relate.follower_id = user.user_id','LEFT')
                ->field(array('follower_id','user_name','message','evaluate'))
                ->where(array('mission_id'=>$mission_id,'is_selected'=>2))->select();
            $finish_users = $mission_user_db->join('user on miss_user_relate.follower_id = user.user_id','LEFT')
                ->field(array('follower_id','user_name','message','evaluate'))
                ->where(array('mission_id'=>$mission_id,'is_selected'=>3))->select();
            /*$users_array = array();
            foreach($selected_users as $user){
                $users_array[] = $user['user_name'];
            }
            $users = implode(',',$users_array);*/

            //$data = array();

            if(sizeof($list)==1){
                $info = $list[0];
                $info['apply'] = $apply_users;
                $info['select'] = $selected_users;
                $info['finish'] = $finish_users;
            }
            $this->assign('info', $info);

            $this->display('mission_view');
        }
    }


    public function missionDelete($mission_id){
        $mission_db = M('mission');
        $result = $mission_db->where(array('mission_id'=>$mission_id))->delete();
        if ($result){
            $this->success('删除成功');
        }else {
            $this->error('删除失败');
        }
    }

     public function toTop($id){
         $mission_db = M('mission');
         $result = $mission_db->where(array('mission_id'=>$id))->select();
         if(sizeof($result)==1){
             $mission = $result[0];
             if($mission['totop']==0||$mission['totop']==null){

                 $mission['totop'] = $mission_db->max('totop')+1;
                 $mission_db->where(array('mission_id'=>$id))->save($mission);
                 $this->success('操作成功');
             }else{
                 $mission['totop'] = 0;
                 $mission_db->where(array('mission_id'=>$id))->save($mission);
                 $this->success('操作成功');
             }
         }else{
             $this->error('操作失败');
         }
     }
} 